package com.guli.common.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;

public class ImageUtils {

    private static final int width = 200;
    private static final int height = 150;

    public static byte[] generateCaptcha(String source) {
        try{
            BufferedImage bufferedImage = new BufferedImage(width,height,1);
            Graphics graphics = bufferedImage.getGraphics();
            graphics.setColor(Color.yellow);
            graphics.drawString(source,width/source.length(),20);
            graphics.dispose();
            // 将图像写入字节数组
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "PNG", byteArrayOutputStream);
            // 返回字节数组
            return byteArrayOutputStream.toByteArray();
        }catch (Exception ex){
            System.out.println(ex);
        }
        return null;
    }


}
